//
//  DCFUserInfoView.m
//  DCFactory
//
//  Created by 吴文拼 on 2018/3/30.
//  Copyright © 2018年 吴文拼. All rights reserved.
//

#import "DCFUserInfoView.h"
#import "DCFLoginManager.h"

@interface DCFUserInfoView ()

@property (nonatomic , strong)DCFNormalView *leftView;

@property (nonatomic , strong)DCFNormalImageView *headImV;

@property (nonatomic , strong)DCFNormalLabel *phoneNumLab;

@end

@implementation DCFUserInfoView

-(instancetype)init{
    if (self = [super init]) {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = RGBACOLOR(0, 0, 0, 0.4f);
        [self addSubview:self.leftView];
        DCFNormalView *view = [[DCFNormalView alloc] initWithFrame:CGRectMake(264, 0, SCREEN_WIDTH-264, SCREEN_HEIGHT)];
        [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)]];
        [self addSubview:view];
        
    }
    return self;
}

-(DCFNormalView *)leftView{
    if (!_leftView) {
        _leftView = [[DCFNormalView alloc] initWithFrame:CGRectMake(-264, 0, 264, SCREEN_HEIGHT)];
        [_leftView set_dcf_backgroundColor:[UIColor whiteColor]];
        DCFButton *quitBtn = [DCFButton buttonWithType:UIButtonTypeCustom];
        [quitBtn set_dcf_BackgroundColor:RGBCOLOR(48, 130, 253) forState:UIControlStateNormal];
        [quitBtn set_dcf_AttributedTitle:[[NSAttributedString alloc] initWithString:@"退出" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:GetFontWithSize(15)}] forState:UIControlStateNormal];
        [quitBtn addTarget:self action:@selector(quitBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_leftView addSubview:quitBtn];
        [quitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(_leftView);
            make.height.equalTo(@48);
        }];
        [_leftView addSubview:self.headImV];
        [_headImV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_leftView);
            make.top.equalTo(_leftView).offset(STATUS_BAR_HEIGHT+54.0f);
            make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(41, 41)]);
        }];
        [_leftView addSubview:self.phoneNumLab];
        [_phoneNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_headImV.mas_bottom).offset(6);
            make.centerX.equalTo(_headImV);
            make.left.right.equalTo(_leftView);
            make.height.equalTo(@20);
        }];
        DCFNormalView *rowView1 = [self getViewWithImage:[UIImage imageNamed:@"feedback_icon"] andText:@"意见反馈"];
        [_leftView addSubview:rowView1];
        [rowView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_phoneNumLab.mas_bottom).offset(55);
            make.left.right.equalTo(_leftView);
            make.height.equalTo(@35);
        }];
        DCFNormalImageView *arrowImV = [[DCFNormalImageView alloc] initWithImage:[UIImage imageNamed:@"right_arrow"]];
        [rowView1 addSubview:arrowImV];
        [arrowImV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(rowView1).offset(-12);
            make.centerY.equalTo(rowView1);
            make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(8, 15.0f)]);
        }];
        DCFNormalView *rowView2 = [self getViewWithImage:[UIImage imageNamed:@"version_icon"] andText:@"版本号"];
        [_leftView addSubview:rowView2];
        [rowView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(rowView1.mas_bottom);
            make.left.right.equalTo(_leftView);
            make.height.equalTo(@35);
        }];
        DCFNormalLabel *versionLab = [[DCFNormalLabel alloc] init];
        [versionLab set_dcf_font:GetFontWithSize(15)];
        [versionLab set_dcf_textColor:RGBCOLOR(153, 153, 153)];
        [versionLab set_dcf_textAligment:NSTextAlignmentRight];
        versionLab.text = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
        [rowView2 addSubview:versionLab];
        [versionLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(rowView2);
            make.right.equalTo(rowView2).offset(-12);
            make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(35, 21)]);
        }];
        
        [rowView1 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickRowOne)]];
        [rowView2 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickRowTwo)]];
    }
    return _leftView;
}

-(DCFNormalView *)getViewWithImage:(UIImage *)image andText:(NSString *)text{
    DCFNormalView *view = [[DCFNormalView alloc] init];
    DCFNormalImageView *imV = [[DCFNormalImageView alloc] initWithImage:image];
    [view addSubview:imV];
    [imV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view).offset(21);
        make.centerY.equalTo(view);
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(18, 18)]);
    }];
    DCFNormalLabel *lab = [[DCFNormalLabel alloc] init];
    [lab set_dcf_textAligment:NSTextAlignmentLeft];
    [lab set_dcf_textColor:RGBCOLOR(51, 51, 51)];
    [lab set_dcf_font:GetFontWithSize(15)];
    lab.text = text;
    [view addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imV.mas_right).offset(8);
        make.centerY.equalTo(view);
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(70, 21)]);
    }];
    DCFNormalView *lineV = [[DCFNormalView alloc] init];
    [lineV set_dcf_backgroundColor:RGBCOLOR(242, 242, 242)];
    [view addSubview:lineV];
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view).offset(11);
        make.right.equalTo(view).offset(-11);
        make.bottom.equalTo(view);
        make.height.equalTo(@1.0f);
    }];
    return view;
}

-(DCFNormalImageView *)headImV{
    if (!_headImV) {
        _headImV = [[DCFNormalImageView alloc] init];
        [_headImV set_dcf_backgroundColor:RGBCOLOR(216, 216, 216)];
        [_headImV set_dcf_layerCornerRadius:20.5f];
        _headImV.clipsToBounds = YES;
    }
    return _headImV;
}

-(DCFNormalLabel *)phoneNumLab{
    if (!_phoneNumLab) {
        _phoneNumLab = [[DCFNormalLabel alloc] init];
        [_phoneNumLab set_dcf_font:GetFontWithSize(14)];
        [_phoneNumLab set_dcf_textColor:RGBCOLOR(51, 51, 51)];
        [_phoneNumLab set_dcf_textAligment:NSTextAlignmentCenter];
        _phoneNumLab.text = @"188888888";
    }
    return _phoneNumLab;
}

-(void)show{
    self.phoneNumLab.text = [[DCFLoginManager sharedInstance] currentUser].mobile;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.3f animations:^{
        self.leftView.frame = CGRectMake(0, 0, 264, SCREEN_HEIGHT);
    } completion:^(BOOL finished) {
        
    }];
}

-(void)hide{
    [UIView animateWithDuration:0.3f animations:^{
        self.leftView.frame = CGRectMake(-264, 0, 264, SCREEN_HEIGHT);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)clickRowOne{
    [UIView animateWithDuration:0.3f animations:^{
        self.leftView.frame = CGRectMake(-264, 0, 264, SCREEN_HEIGHT);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        if (_feedbackBlock) {
            _feedbackBlock();
        }
    }];
}

-(void)clickRowTwo{
    
}

-(void)quitBtnClick{
    [[DCFLoginManager sharedInstance] logoutWithCompleteBlock:^(BOOL success) {
        [self hide];
    }];
}


@end
